﻿using DAO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using BALayer;

namespace QuanLyDoiTuong
{
    public partial class FrmChangePassWord : Form
    {
        private UserDAO userLogin = new UserDAO();
        public FrmChangePassWord()
        {
            InitializeComponent();
        }
        public FrmChangePassWord(UserDAO userLogin)
        {
            this.userLogin = userLogin;
            InitializeComponent();
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            UnicodeEncoding encoding = new UnicodeEncoding();
            Byte[] hashBytes = encoding.GetBytes(txtPassCu.Text);
            Byte[] cryptBytes = sha1.ComputeHash(hashBytes);
            String passCu = BitConverter.ToString(cryptBytes);
            if (!passCu.Equals(userLogin.PassWord))
            {
                MessageBox.Show("Mật khẩu không chính xác!", "Thông Báo");
                txtPassCu.Focus();
            }
            else if (!txtPassMoi.Text.Equals(txtPassMoiConfirm.Text))
            {
                MessageBox.Show("Mật khẩu mới không giống nhau!", "Thông Báo");
                txtPassMoi.Focus();
            }
            else
            {
                hashBytes = encoding.GetBytes(txtPassMoi.Text);
                cryptBytes = sha1.ComputeHash(hashBytes);
                userLogin.PassWord = BitConverter.ToString(cryptBytes);
                try
                {
                    int result = UserBA.changePassWord(userLogin);
                    if (1 == result)
                    {
                        MessageBox.Show("Đổi mật khẩu thành công!", "Thông Báo");
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông Báo");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
